package com.xing.fruit.servlet;

import com.xing.fruit.pojo.User;
import com.xing.fruit.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String request = req.getParameter("request");
        if (request.equals("add")){
            this.addUser( req, resp);   //添加用户
        }else if (request.equals("delete")){
            this.deleteUserById(req,resp);   //删除用户
        }else if (request.equals("update")){
            this.updateUserById(req,resp);   //修改用户
        }else if (request.equals("updatePwd")){
            this.updateUserByPwd(req,resp);   //修改用户密码
        }else if (request.equals("select")){
            this.getUserList(req,resp);   //查询全部用户
        }else if (request.equals("selectById")){
            this.selectUserById(req,resp);   //通过Id查询用户
        }
    }
    //添加用户
    public void addUser(HttpServletRequest req, HttpServletResponse resp){
        String nickname = req.getParameter("nickname");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //默认为123456.可以修改
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        User user = new User();
        user.setNickname(nickname);
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(sex);
        user.setPhone(phone);
        user.setEmail(email);
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.userReg(user);
        if (i>0){
            this.getUserList(req, resp);
        }else {
            try {
                resp.sendRedirect("/jsp/user_add.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //删除用户
    public void deleteUserById(HttpServletRequest req, HttpServletResponse resp){
        int uid = Integer.parseInt(req.getParameter("uid"));
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.deleteUser(uid);
        if (i>0){
            try {
                req.getRequestDispatcher("/user?request=select").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                resp.sendRedirect("/jsp/user_list.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    //修改用户
    public void updateUserById(HttpServletRequest req, HttpServletResponse resp){
        //从前台获取参数
        int uid = Integer.parseInt(req.getParameter("uid"));
        String nickname = req.getParameter("nickname");
        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");

        //通过set传值
        User user = new User();
        user.setUid(uid);
        user.setNickname(nickname);
        user.setUsername(username);
        user.setSex(sex);
        user.setPhone(phone);
        user.setEmail(email);
        //调用service层
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.updateUser(user);
        if (i>0){
            try {
                try {
                    req.getRequestDispatcher("/user?request=selectById&uid="+uid).forward(req,resp);
                } catch (ServletException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                req.getRequestDispatcher("/jsp/information.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //修改密码
    public void updateUserByPwd(HttpServletRequest req, HttpServletResponse resp){
        String password = req.getParameter("updatePwd");
        String username = req.getParameter("username");
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.updatePwd(password, username);
        if (i>0){
            try {
                req.getRequestDispatcher("/jsp/information.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                req.getRequestDispatcher("/jsp/password.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //查询用户通过Id
    public void selectUserById(HttpServletRequest req, HttpServletResponse resp){
        int uid = Integer.parseInt(req.getParameter("uid"));
        UserServiceImpl userService = new UserServiceImpl();
        User userById = userService.getUserById(uid);
        if (userById != null){
            //更新登录用户信息
            HttpSession session = req.getSession();
            session.setAttribute("user",userById);
            try {
                req.getRequestDispatcher("/jsp/information.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //查询所有用户
    public void getUserList(HttpServletRequest req, HttpServletResponse resp){
        UserServiceImpl userService = new UserServiceImpl();
        List<User> userList = userService.getUserList();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        //判断是否为管理员用户
        if (username.equals("admin")){
            session.setAttribute("userList",userList);
            try {
//                req.getRequestDispatcher("/jsp/user_list.jsp").forward(req,resp);
                resp.sendRedirect("/jsp/user_list.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                resp.sendRedirect("error.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
